﻿#region Software License
/**************************************************************/
/*          Business of Software Consulting                   */
/*          FREE SOFTWARE LICENSE                             */
/**************************************************************/
/*  Directory Manifest Generator                              */
/*  by: Mike Cating                                           */
/*  © 2009 Business of Software Consulting                    */
/*                                                            */
/* BUSINESS OF SOFTWARE CONSULTING DISCLAIMS ALL WARRANTIES,  */
/* RESPONSIBILITIES, AND LIABILITIES ASSOCIATED WITH          */
/* USE OF THIS CODE IN ANY WAY, SHAPE, OR FORM                */
/* REGARDLESS HOW IMPLICIT, EXPLICIT, OR OBSCURE IT           */
/* IS. IF THERE IS ANYTHING QUESTIONABLE WITH REGARDS         */
/* TO THIS SOFTWARE BREAKING AND YOU GAIN A LOSS OF           */
/* ANY NATURE, WE ARE NOT THE RESPONSIBLE PARTY. USE          */
/* OF THIS SOFTWARE CREATES ACCEPTANCE OF THESE TERMS         */
/*                                                            */
/* USE OF THIS CODE MUST RETAIN ALL COPYRIGHT NOTICES         */
/* AND LICENSES (MEANING THIS TEXT).                          */
/*                                                            */
/**************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Serialization;
using System.Globalization;

namespace BOSTools.DirectoryManifestGenerator
{
    public class DirectoryManifest
    {
        private string _directoryName;
        private List<FileDetail> _fileDetails = new List<FileDetail>();
        private List<DirectoryDetail> _directoryNames = new List<DirectoryDetail>();

        public string DirectoryName
        {
            get { return _directoryName; }
            set { _directoryName = value; }
        }

        [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
        public FileDetail[] FileDetails
        {
            get { return _fileDetails.ToArray(); }
            set { _fileDetails = new List<FileDetail>(value); }
        }

        [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
        public DirectoryDetail[] Subdirectories
        {
            get { return _directoryNames.ToArray(); }
            set { _directoryNames = new List<DirectoryDetail>(value); }
        }

        public void AddFileDetail(FileDetail detail)
        {
            _fileDetails.Add(detail);
        }

        public void AddDirectory(string directoryName)
        {
            if (!string.IsNullOrEmpty(directoryName))
            {
                _directoryNames.Add(new DirectoryDetail( directoryName ));
            }
        }

        [XmlIgnore]
        public int TotalFiles
        {
            get
            {
                return _fileDetails.Count;
            }
        }

        public bool ContainsFile(string relativeFilePath)
        {
            FileDetail detail = FindRelativeFileName(relativeFilePath);
            if (detail == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public FileDetail FindRelativeFileName(string relativeFilePath)
        {
            FileDetail file = _fileDetails.Find( delegate( FileDetail detail )
                    {
                        if (0 == string.Compare(relativeFilePath, detail.RelativeFileName, 
                                                StringComparison.OrdinalIgnoreCase))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        };
                    });

            return file;
        }

        public bool ContainsDirectory(string directory)
        {
            DirectoryDetail foundDir = _directoryNames.Find(delegate(DirectoryDetail current)
            {
                if (0 == string.Compare(directory, current.RelativeDirectoryPath,
                                                StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                else
                {
                    return false;
                };
            });

            if (foundDir != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
